﻿using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XNAProject.GameElements;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using XNAProjectLibrary;
using XNAProject.Core;
using XNAProject.Ships;
using XNAProject.Enumerations;

namespace XNAProject
{
    public sealed class PlayState : BaseGameState, IPlayState
    {
        private SpriteFont font;

        private GameComponentCollection componentCollection;

        private Level level;
        private ConsoleMenu consoleMenu;
        private Chat chat;
        private XNAProjectCamera camera;

        private Player player;
        private Enemy enemy;

        public PlayState(Game game)
            : base(game)
        {
            game.Services.AddService(typeof(IPlayState), this);

            componentCollection = (GameComponentCollection)game.Services.GetService(typeof(GameComponentCollection));
            camera = (XNAProjectCamera)Game.Services.GetService(typeof(XNAProjectCamera));        
            level = (Level)Game.Services.GetService(typeof(Level));
            player = (Player)Game.Services.GetService(typeof(Player));

            level.active = true;

            enemy = (Enemy)Game.Services.GetService(typeof(Enemy));

            enemy.AddShip(new Cruiser(Game, ShipFaceing.Down, new Vector2(15, 0)));
            enemy.AddShip(new Cruiser(Game, ShipFaceing.Left, new Vector2(15, 1)));
            enemy.AddShip(new Cruiser(Game, ShipFaceing.Left, new Vector2(15, 2)));
            enemy.AddShip(new Cruiser(Game, ShipFaceing.Left, new Vector2(15, 3)));

            foreach (Ship ship in enemy)
            {
                ship.visible = false;
                componentCollection.Add(ship);
            }
            if (player.host)
                player.turn = true;
            else player.turn = false;

            consoleMenu = new ConsoleMenu(Game);
            componentCollection.Add(consoleMenu);
            Game.Services.AddService(typeof(ConsoleMenu), consoleMenu);
            game.Components.Add(consoleMenu);

            chat = new Chat(Game);
            componentCollection.Add(chat);
            Game.Services.AddService(typeof(Chat), chat);
            game.Components.Add(chat);

            foreach (GameComponent component in componentCollection)
            {
                if (!Game.Components.Contains(component))
                    Game.Components.Add(component);
            }
        }

        public override void Initialize()
        {
            //Game.GraphicsDevice.VertexDeclaration = new VertexDeclaration(Game.GraphicsDevice, VertexPositionNormalTexture.VertexElements);

            //Game.GraphicsDevice.RenderState.CullMode = CullMode.None;         

            base.Initialize();
        }

        protected override void LoadContent()
        {         
            font = Game.Content.Load<SpriteFont>(@"Fonts\description");

            base.LoadContent();
        }

        public void UnloadComponents()
        {            
            foreach (GameComponent component in componentCollection)
            {
                Game.Components.Remove(component);
            }  

            Game.Services.RemoveService(typeof(IPlayState));
            Game.Services.RemoveService(typeof(XNAProjectCamera));
            Game.Services.RemoveService(typeof(Enemy));
            player.ReSet();
            Game.Services.RemoveService(typeof(Level));
            Game.Services.RemoveService(typeof(ConsoleMenu));
            Game.Services.RemoveService(typeof(GameComponentCollection));         
        }

        public override void Update(GameTime gameTime)
        {
            if (Input.WasPressed(0, Buttons.Start, Keys.Escape))
            {
                GameManager.PushState(OurGame.FloatMenuState.Value);
            }
            else if (Input.WasPressed(0, Buttons.DPadRight, Keys.LeftControl))
            {
                GameManager.ChangeState(OurGame.ResultState.Value);
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            OurGame.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

            OurGame.spriteBatch.DrawString(font, @"PlayState \n press ESC for floatMenuState \n Use AWSD and mouse for moving 
                \n press Space for ResultState \n Use mouse for highlighting \n Click for selecting 
                \n RightClick for Moving, or Atacking \n Use O for OilSpill"
                , new Vector2(20, 20), Color.White);

            OurGame.spriteBatch.End();

            base.Draw(gameTime);
        }


    }
}
